<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-touch-fullscreen" content="yes">
		<meta name="format-detection" content="telephone=no;email=no">

		<link rel="stylesheet" href="css/mui.min.css">
		<link rel="stylesheet" href="css/iconfont.css">
		<link rel="stylesheet" href="css/style.css">
		<title>主页</title>
		<style>
			.mui-bar-tab .mui-tab-item.mui-active {
				color: #159ceb;
			}
		</style>
	</head>

	<body>
		<nav class="mui-bar mui-bar-tab">
			<a id="defaultTab" class="mui-tab-item mui-active" href="html/home/home.html">
				<span class="mui-icon iconfont">&#xe604;</span>
				<span class="mui-tab-label">首页</span>
			</a>
			<a class="mui-tab-item" href="html/lkgyx/yx.html">
				<span class="mui-icon iconfont">&#xe61a;</span>
				<span class="mui-tab-label">印象</span>
			</a>
			<a class="mui-tab-item" href="html/my_center/my_center.html">
				<span class="mui-icon iconfont">&#xe606;</span>
				<span class="mui-tab-label">我的</span>
			</a>
		</nav>
		<input id="main" type="hidden" value="0">
		<script src="js/mui.min.js"></script>
		<script src="js/common.js"></script>
		<script>
			//mui初始化
			mui.init({swipeBack: false});
			var subpages = ['html/home/home.html', 'html/lkgyx/yx.html', 'html/my_center/my_center.html'];
			var subpage_style = {
				top: '0px',
				bottom: '50px'
			};
			var aniShow = {};
			//创建子页面，首个选项卡页面显示，其它均隐藏
			var wgtVer = null;
			mui.plusReady(function() {
				// 获取本地应用资源版本号
			    plus.runtime.getProperty(plus.runtime.appid,function(inf){
			        wgtVer = parseFloat(inf.version);
			    });
			    // 检查更新
				checkUpdate();
				var uuid = plus.device.uuid; //获取设备的唯一标识
				var self = plus.webview.currentWebview();
				for (var i = 0; i < 3; i++) {
					var temp = {};
					var sub = plus.webview.create(subpages[i], subpages[i], subpage_style);
					if (i > 0) {
						sub.hide();
					} else {
						temp[subpages[i]] = "true";
						mui.extend(aniShow, temp);
						/*sub.addEventListener('loaded', function() {
	                        plus.navigator.closeSplashscreen();
	                    });*/
					}
					self.append(sub);
				}
			});
			
			function checkUpdate(){
				var checkUrl = null;
				if(mui.os.ios){
					checkUrl = baseUrl + "/devs/appVersion";
				}
				else if(mui.os.android){
					checkUrl = baseUrl + "/devs/appVersionAZ";
				}
				
			    mui.ajax({
					url: checkUrl,
					dataType: 'json',
					type: 'get',
					success: function(data) {
						if (data.returnFlag == 0) {
							var newVer = parseFloat(data.version);
							
			                if(wgtVer && newVer && (wgtVer < newVer)){
			                	mui.confirm('检测到新版本，是否立即下载更新？', '版本更新', btnArray, function(e) {
									if (e.index == 0) {
										downWgt(data.download);  // 下载升级包
									}
								});
			                }
						}
					},
					error: function(xhr, type, errorThrown) {
						//异常处理；
					}
				});
			}
			var btnArray = ['确认', '取消'];
			// 下载wgt文件
			function downWgt(wgtUrl){
				plus.nativeUI.showWaiting("下载更新文件...", {width: "50px", height: "50px", background: "rgba(255, 255, 255, 0)", style: "black"});
			    plus.downloader.createDownload( wgtUrl, {filename:"_doc/update/"}, function(d,status){
			        if ( status == 200 ) { 
//			            console.log("下载更新成功："+d.filename);
			            installWgt(d.filename); // 安装wgt包
			        } else {
//			            console.log("下载更新失败！");
			            plus.nativeUI.alert("下载更新失败！");
			        }
			        plus.nativeUI.closeWaiting();
			    }).start();
			}
			
			// 更新应用资源
			function installWgt(path){
				plus.nativeUI.showWaiting("安装更新文件...", {width: "50px", height: "50px", background: "rgba(255, 255, 255, 0)", style: "black"});
			    plus.runtime.install(path,{force : true},function(){
			        plus.nativeUI.closeWaiting();
			        console.log("安装更新文件成功！");
			        plus.nativeUI.alert("应用资源更新完成！",function(){
			            plus.runtime.restart();
			        });
			    },function(e){
			        plus.nativeUI.closeWaiting();
//			        console.log("安装wgt文件失败["+e.code+"]："+e.message);
			        plus.nativeUI.alert("安装更新文件失败["+e.code+"]："+e.message);
			    });
			}

			
			//当前激活选项
			var activeTab = subpages[0];
			//选项卡点击事件
			mui('.mui-bar-tab').on('tap', 'a', function(e) {
				var targetTab = this.getAttribute('href');
				if (targetTab == activeTab) {
					return;
				}
				var modeFlag = localStorage.getItem("modeFlag");
				if (modeFlag) {
					plus.navigator.setStatusBarStyle("UIStatusBarStyleBlackOpaque");
						plus.navigator.setStatusBarBackground("#000");
				} 
				else {
					if (targetTab == subpages[0]) {
						plus.navigator.setStatusBarStyle("UIStatusBarStyleDefault");
						plus.navigator.setStatusBarBackground("#fefefe");
					} else if (targetTab == subpages[1]) {
						plus.navigator.setStatusBarStyle("UIStatusBarStyleBlackOpaque");
						plus.navigator.setStatusBarBackground("#0173d8");
					} else {
						plus.navigator.setStatusBarStyle("UIStatusBarStyleBlackOpaque");
						plus.navigator.setStatusBarBackground("#013b7d");
					}
				}
				//显示目标选项卡
				//若为iOS平台或非首次显示，则直接显示
				if (mui.os.ios || aniShow[targetTab]) {
					plus.webview.show(targetTab);
				} else {
					//否则，使用fade-in动画，且保存变量
					var temp = {};
					temp[targetTab] = "true";
					mui.extend(aniShow, temp);
					plus.webview.show(targetTab, "fade-in", 300);
				}
				//隐藏当前;
				plus.webview.hide(activeTab);
				//更改当前活跃的选项卡
				activeTab = targetTab;
			});
			//添加night自定义事件监听
			/*window.addEventListener('night', function(event) {
				lkg.nightMode();
			});*/
			/**
			 * 首页返回键处理
			 */
			//处理逻辑：1秒内，连续两次按返回键，则退出应用；
			var first = null;
			mui.back = function() {
				//首次按键，提示‘再按一次退出应用’
				if (!first) {
					first = new Date().getTime();
					mui.toast('再按一次退出应用');
					setTimeout(function() {
						first = null;
					}, 1000);
				} else {
					if (new Date().getTime() - first < 1000) {
						plus.runtime.quit();
					}
				}
			};
		</script>
	</body>

</html>